
package TransportesMarroquin;

import com.mysql.jdbc.Connection;

import java.awt.event.ActionEvent; 
import java.awt.event.MouseEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextField;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.FileNotFoundException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;


public class NuevoViajeDatos extends javax.swing.JFrame {

 int enviarcod;   // dodigo del viaje 
 int ano,mes,dia; // para ingresar la fecha 
 String fecha;
 boolean error=false;
 int resultado;
 static int cbulto;
 static boolean gastoviaje= false;
 ButtonGroup bc;
 int cvehiculo;
 String tipog="Viaje";
 String tvehiculo;
    static  int kilometraje;
    public NuevoViajeDatos(int cod,int codvehiculo, String tipovehiculo) {
        initComponents();
        cvehiculo=codvehiculo;
        tvehiculo=tipovehiculo;
        enviarcod=cod;
        bc = new ButtonGroup();
        bc.add(bsi);
        bc.add(bno);
        Fecha();
        
    }

    private NuevoViajeDatos() {
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        ckil = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        cvul = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        cdes = new javax.swing.JTextField();
        cori = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        cano = new javax.swing.JTextField();
        cmes = new javax.swing.JTextField();
        cdia = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        bno = new javax.swing.JRadioButton();
        bsi = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(143, 0, 0));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nuevo Viaje ");
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(215, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/regresar.png"))); // NOI18N
        jButton1.setText(" Regresar ");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        ckil.setName("ckil"); // NOI18N
        ckil.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ckilActionPerformed(evt);
            }
        });
        ckil.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ckilKeyTyped(evt);
            }
        });

        jLabel16.setText("Gastos");
        jLabel16.setName("jLabel16"); // NOI18N

        jLabel15.setText("Fecha");
        jLabel15.setName("jLabel15"); // NOI18N

        jLabel13.setText("V I A J E");
        jLabel13.setName("jLabel13"); // NOI18N

        cvul.setName("cvul"); // NOI18N
        cvul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cvulActionPerformed(evt);
            }
        });
        cvul.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cvulKeyTyped(evt);
            }
        });

        jLabel5.setText("Cantidad Bulto");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel18.setText("Kilometraje");
        jLabel18.setName("jLabel18"); // NOI18N

        cdes.setName("cdes"); // NOI18N
        cdes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cdesActionPerformed(evt);
            }
        });
        cdes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                cdesKeyTyped(evt);
            }
        });

        cori.setName("cori"); // NOI18N
        cori.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                coriActionPerformed(evt);
            }
        });
        cori.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                coriKeyTyped(evt);
            }
        });

        jLabel8.setText("Destino");
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel6.setText("Origen");
        jLabel6.setName("jLabel6"); // NOI18N

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/siguiente.png"))); // NOI18N
        jButton4.setText("Siguiente");
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        cano.setText("2011");
        cano.setBorder(null);
        cano.setName("cano"); // NOI18N

        cmes.setText("12");
        cmes.setBorder(null);
        cmes.setName("cmes"); // NOI18N

        cdia.setText("31");
        cdia.setBorder(null);
        cdia.setName("cdia"); // NOI18N

        jLabel7.setText("/");
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel9.setText("/");
        jLabel9.setName("jLabel9"); // NOI18N

        bno.setSelected(true);
        bno.setText("No");
        bno.setName("bno"); // NOI18N
        bno.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bnoMouseClicked(evt);
            }
        });

        bsi.setText("Si");
        bsi.setName("bsi"); // NOI18N
        bsi.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bsiMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(148, 148, 148)
                .addComponent(jLabel15)
                .addGap(29, 29, 29)
                .addComponent(cdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLabel7)
                .addGap(10, 10, 10)
                .addComponent(cmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jLabel9)
                .addGap(7, 7, 7)
                .addComponent(cano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel13))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel6)
                .addGap(51, 51, 51)
                .addComponent(cori, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel8)
                .addGap(47, 47, 47)
                .addComponent(cdes, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel5)
                .addGap(13, 13, 13)
                .addComponent(cvul, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel18)
                .addGap(30, 30, 30)
                .addComponent(ckil, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel16)
                .addGap(50, 50, 50)
                .addComponent(bsi)
                .addGap(17, 17, 17)
                .addComponent(bno))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addGap(81, 81, 81)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel15)
                    .addComponent(cdia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(cmes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(cano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabel13)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel6))
                    .addComponent(cori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel8))
                    .addComponent(cdes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel5))
                    .addComponent(cvul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel18))
                    .addComponent(ckil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel16))
                    .addComponent(bsi)
                    .addComponent(bno))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jButton4, 0, 0, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        NuevoViajeCliente.va.show();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void ckilActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ckilActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_ckilActionPerformed

    private void cvulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cvulActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cvulActionPerformed

    private void cdesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cdesActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cdesActionPerformed

    private void coriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_coriActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_coriActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        error = false;
        // despues qee verifique que los datos esten bien los guardare en nuevo cliente.
        
        
           try {
                Insertar();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(NuevoCabezal.class.getName()).log(Level.SEVERE, null, ex);
            }
      
        
        
        if (!error){
            if(!gastoviaje){
                NuevoViajePiloto.va.dispose();
           // Regreso a todos los viajes. 
           
            NuevoViajeVulto va = new NuevoViajeVulto (NuevoViajeCliente.enviarcod);
            va.setVisible(true);
   
            }
            if(gastoviaje){
                NuevoViajePiloto.va.dispose();
                NuevoGasto va = new NuevoGasto(enviarcod,tipog);
                va.setVisible(true);
            }
            
            cbulto=Integer.parseInt(cvul.getText());
           
         
            
        }
}//GEN-LAST:event_jButton4ActionPerformed

    private void bnoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bnoMouseClicked
        if(bno.isSelected()) {
           NuevoViajeDatos.gastoviaje=false;
        }
}//GEN-LAST:event_bnoMouseClicked

    private void bsiMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bsiMouseClicked
        if(bsi.isSelected()) {
        NuevoViajeDatos.gastoviaje=true;
        }
}//GEN-LAST:event_bsiMouseClicked

    private void coriKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_coriKeyTyped
             char c = evt.getKeyChar();
      if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || ((c== KeyEvent.VK_SPACE)))) {
            getToolkit().beep();
            evt.consume();
}
    }//GEN-LAST:event_coriKeyTyped

    private void cdesKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cdesKeyTyped
            char c = evt.getKeyChar();
      if (!(Character.isLetter(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE) || ((c== KeyEvent.VK_SPACE)))) {
            getToolkit().beep();
            evt.consume();
}
    }//GEN-LAST:event_cdesKeyTyped

    private void cvulKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cvulKeyTyped
               char c = evt.getKeyChar();
      if (!(Character.isDigit(c) || (c == evt.VK_BACK_SPACE) || (c == evt.VK_DELETE) || ((c== evt.VK_SPACE)))) {
            getToolkit().beep();
            evt.consume();
}
    }//GEN-LAST:event_cvulKeyTyped

    private void ckilKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ckilKeyTyped
               char c = evt.getKeyChar();
      if (!(Character.isDigit(c) || (c == evt.VK_BACK_SPACE) || (c == evt.VK_DELETE) || ((c== evt.VK_SPACE)))) {
            getToolkit().beep();
            evt.consume();
}
    }//GEN-LAST:event_ckilKeyTyped
public void Insertar() throws FileNotFoundException{
     
    try {
        // en caso que haya cambiado la fecha   
      fecha=cano.getText().toString()+"-"+cmes.getText().toString()+"-"+cdia.getText().toString();
         

            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("");
            dataSource.setDatabaseName("practicasp8");
            dataSource.setServerName("localhost");
           
            // coneccion
            Connection conexion = (Connection) dataSource.getConnection();

            PreparedStatement ps = null;
            ps = (PreparedStatement) conexion.prepareStatement("UPDATE viaje set origen=?, destino=?, vulto=?, fecha=?,kilometraje=? where idviaje = ?");
// PRIMER CAMPO 
            ps.setString(1,cori.getText());
          
 // SEGUNDO CAMPO 
            ps.setString(2,cdes.getText());
            
// TERCER CAMPO 
             try{
            ps.setInt(3,Integer.parseInt(cvul.getText())); 
           
        
         }catch (Exception e) {
             JOptionPane.showMessageDialog(rootPane, "Dato erroneo en campo vulto, no es un dato númerico.");
         }
            
// CUARTO CAMPO 
            ps.setDate(4,Date.valueOf(fecha));
            
            
// QUINTO CAMPO 
              try{
                  
            ps.setInt(5,Integer.parseInt(ckil.getText())); 
           kilometraje = Integer.parseInt(ckil.getText());
        
         }catch (Exception e) {
             kilometraje = 0;
             JOptionPane.showMessageDialog(rootPane, "Dato erroneo en el campo del kilometraje, no es un dato númerico.");
         }
// SEXTO CAMPO 
         try{
            ps.setInt(6,enviarcod); 
            
        
         }catch (Exception e) {
             JOptionPane.showMessageDialog(rootPane, "Dato erroneo en el codigo del viaje");
         }
              
      
         
             resultado = ps.executeUpdate();


               conexion.close(); // cierro la conexion
               ps.close();

             }catch(SQLException e){
             JOptionPane.showMessageDialog(rootPane,"Error en Programa:"+e);
         
           
            error = true;
            
           } // fin del catch
}  
    public void Fecha(){
         // fecha actual 
        GregorianCalendar calendario = new GregorianCalendar();
     
//Hacer formateador de la fecha, se le pasa el formato en que se quiere obtener la fecha.
SimpleDateFormat df = new SimpleDateFormat("dd"); 
cdia.setText(""+df.format(calendario.getTime()));
df=new SimpleDateFormat("MM");
cmes.setText(""+df.format(calendario.getTime()));
df=new SimpleDateFormat("yyyy");
cano.setText(""+df.format(calendario.getTime()));

        

    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NuevoViajeDatos().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton bno;
    private javax.swing.JRadioButton bsi;
    private javax.swing.JTextField cano;
    private javax.swing.JTextField cdes;
    private javax.swing.JTextField cdia;
    private javax.swing.JTextField ckil;
    private javax.swing.JTextField cmes;
    private javax.swing.JTextField cori;
    private javax.swing.JTextField cvul;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
